package com.fa4j.system.app.controller;

import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.Page;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.common.web.annotation.ApiMethod;
import com.fa4j.common.web.model.AppType;
import com.fa4j.system.app.model.AppRole;
import com.fa4j.system.app.model.AppRoleCreateCmd;
import com.fa4j.system.app.model.AppRoleModifyCmd;
import com.fa4j.system.app.model.AppRoleQuery;
import com.fa4j.system.app.repository.AppRoleRepository;
import com.fa4j.system.app.service.AppRoleService;
import com.fa4j.system.login.util.LoginUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/app/role")
@RequiredArgsConstructor
public class AppRoleController {

    private final AppRoleService appRoleService;
    private final AppRoleRepository appRoleRepository;

    @ApiMethod(name = "系统应用角色-分页查询", type = AppType.MANAGE)
    @GetMapping("page")
    public Page<AppRole> page(@Valid AppRoleQuery query) {
        return appRoleRepository.queryPage(query);
    }

    @ApiMethod(name = "系统应用角色-详情查询", type = AppType.MANAGE)
    @GetMapping("{id}")
    public AppRole detail(@PathVariable Integer id) {
        return appRoleRepository.findById(id)
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("系统应用角色"));
    }

    @ApiMethod(name = "系统应用角色-创建", type = AppType.MANAGE)
    @PostMapping
    public AppRole create(@Valid @RequestBody AppRoleCreateCmd cmd) {
        return appRoleService.create(cmd);
    }

    @ApiMethod(name = "系统应用角色-编辑", type = AppType.MANAGE)
    @PatchMapping
    public AppRole modify(@Valid @RequestBody AppRoleModifyCmd cmd) {
        return appRoleService.modify(cmd);
    }

    @ApiMethod(name = "系统应用角色-删除", type = AppType.MANAGE)
    @DeleteMapping("{id}")
    public AppRole remove(@PathVariable Integer id) {
        return appRoleService.remove(RemoveCmd.of(id, LoginUtil.getAccountId()));
    }

}
